package jeecg.business.service.impl.hot;


import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jeecg.business.entity.hot.HotKeyinfoEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.service.hot.HotKeyinfoServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;

@Service("hotKeyinfoService")
@Transactional
public class HotKeyinfoServiceImpl extends CommonServiceImpl implements HotKeyinfoServiceI {
	public void save(String qyid , String keyword , List<HotKeyinfoEntity> hotlist){
		//1、删除相关关键词的热点
		String sql = " delete from NMS_HOT_KEYINFO where qyid = '"+qyid+"' and keyword = '"+keyword+"' ";
		getJdbcTemplate().execute(sql);
		//2、插入热点
		if(hotlist != null){
			for(HotKeyinfoEntity hot : hotlist)
				save(hot);
		}
	}
	
	public List<HotKeyinfoEntity> list(KeyinfoEntity keyinfo){
		String hql = " from HotKeyinfoEntity i where qyid = '"+keyinfo.getQyid()+"' ";
		
		
		
		if(StringUtil.isNotEmpty(keyinfo.getKeywordtype())){
			String sql = " select keyword from nms_key_word where lx = '"+keyinfo.getKeywordtype()+"'";
			List<String> klist = getJdbcTemplate().queryForList(sql , String.class);
			String ids = " ";
			if(klist != null)
				for(String k : klist)
					ids += ","+k;
			ids = "'"+ids.replace(",", "','")+"'";
			hql += " and i.keyword in ("+ids+")  ";
		}
		
		if(StringUtil.isNotEmpty(keyinfo.getStarttime())){
			hql += " and i.endtime >= '"+keyinfo.getStarttime()+"' ";
		}
		if(StringUtil.isNotEmpty(keyinfo.getEndtime())){
			hql += " and i.starttime <= '"+keyinfo.getEndtime()+"' ";
		}
		
		if(StringUtil.isNotEmpty(keyinfo.getKeyword())){
			hql += " and i.keyword like '%"+keyinfo.getKeyword()+"%' ";
		}
		
		hql += " order by appeartime desc , starttime desc ";
		
		return findByQueryString(hql);
	}
}